<template>
  <div class="wrapper">
    <div class="content">
      <div class="title">{{ `${content.deptNameTree}${$route.query.type == 1 ? "平时" : "年度"}考核制度` }}</div>
      <div class="time">更新时间：{{ content.updateTime }}</div>
      <div class="sub-title">制度概述</div>
      <div class="des" v-html="content.assessDescribe.replace(/(\r\n|\n|\r)/gm, '<br />')"></div>
      <div class="sub-title" v-if="content.fileList && content.fileList.length">制度文件</div>
      <div class="file-list" v-if="content.fileList">
        <div class="file-item" v-for="(item, index) in content.fileList" :key="index" @click="downloadFile(item)">
          <img src="@/assets/common/file-icon.png" />
          <div class="file-name van-ellipsis">{{ item.fileName }}</div>
        </div>
      </div>
    </div>
    <van-image-preview v-model="showPreview" :images="images"> </van-image-preview>
  </div>
</template>

<script>
export default {
  props: {
    content: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      previewType: ["jpeg", "gif", "jpg", "png"],
      officType: ["doc", "docx", "ppt", "pptx", "xls", "xlsx"],
      images: [],
      showPreview: false
    };
  },
  methods: {
    downloadFile(item) {
      let arr = item.fileName.split(".");
      let type = arr[arr.length - 1];
      if (this.previewType.includes(type)) {
        this.images = [item.fileUrl];
        this.showPreview = true;
      } else if (this.officType.includes(type)) {
        window.location.href = item.fileUrl.split("?")[0] + "?response-content-type=application/octet-stream";
      } else {
        window.location.href = item.fileUrl;
      }
      // window.location.href = item.fileUrl.split("?")[0] + "?response-content-type=application/octet-stream";

      // var form = document.createElement("form");
      // form.action = item.fileUrl;
      // form.method = "get";
      // form.style.display = "none";
      // document.body.appendChild(form);
      // form.submit();
      // document.body.removeChild(form);

      // const x = new window.XMLHttpRequest();
      // x.open("GET", item.fileUrl, true);
      // x.responseType = "blob";
      // x.onload = () => {
      //   const url = window.URL.createObjectURL(x.response);
      //   const a = document.createElement("a");
      //   a.href = url;
      //   a.download = item.fileName;
      //   a.click();
      // };
      // x.send();
    }
  }
};
</script>

<style scoped lang="scss">
.wrapper {
  padding: 16px;
  .content {
    background: #fff;
    padding: 20px 16px;
    border-radius: 8px;
    line-height: 22px;
    font-size: 14px;
    .title {
      color: #000000;
      font-size: 16px;
      font-weight: 600;
      margin-bottom: 8px;
    }
    .time {
      color: #cccccc;
    }
    .sub-title {
      position: relative;
      margin-top: 16px;
      color: #333333;
      padding-left: 12px;
      &::before {
        content: "";
        position: absolute;
        width: 4px;
        height: 14px;
        top: 4px;
        left: 0;
        border-radius: 1px;
        background: #367fff;
      }
    }
    .des {
      color: #b2b2b2;
      margin-top: 8px;
      font-size: 13px;
      word-break: break-all;
    }
    .file-list {
      margin-top: 16px;
    }
    .file-item {
      display: flex;
      align-items: center;
      border-radius: 8px;
      background: #fafbfc;
      margin-bottom: 12px;
      padding: 14px 16px;
      img {
        display: block;
        width: 26px;
        margin-right: 12px;
      }
      .file-name {
        color: #2a81e6;
        font-size: 13px;
        line-height: 18px;
      }
    }
  }
}
</style>
